﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.loader 
{
	import away3d.loaders.Collada;
	import away3d.loaders.Object3DLoader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author snega
	 */
	public class SceneLoader 
	{
		public var _load3DComplete:Boolean = false;
		public var _load2DComplete:Boolean = false;
		
		public var _modelNum:int = 0;
		public var _modelLoadedNum:int = 0;
		public var _modelLoader:Object3DLoader = null;
		public var _modelXmlList:XMLList = null;
		
		public var _imageNum:int = 0;
		public var _imageLoadedNum:int = 0;
		public var _imageLoader:Loader = null;
		public var _imageXmlList:XMLList = null;
		
		public var _confLoader:URLLoader = null;
		public var _confXml:XML = null;
		
		/* static models */
		public var _modelMap:Object = new Object();
		
		/* static images */
		public var _imageBitmapMap:Object = new Object();
		
		public function SceneLoader(url:String) 
		{
			this._confLoader = new URLLoader(new URLRequest(url));
			this._confLoader.addEventListener(Event.COMPLETE, onConfLoadComplete);
		}
		
		public function onConfLoadComplete(event:Event):void {
			trace("xml configuration loaded");
			
			this._confXml = new XML(this._confLoader.data);
			
			// load 3d object
			this._modelXmlList = this._confXml.load.models;
			
			trace("find " + this._confXml.load.model_number.toString() + " models.");
			
			this._modelNum = parseInt(this._confXml.load.model_number.toString());
			this._modelLoadedNum = 0;
			
			if (this._modelNum > 0) {
				var str:String = this._modelXmlList.model[this._modelLoadedNum].toString()
				
				trace("loading model: " + str);
				this._modelLoader = Collada.load(str);
				this._modelLoader.addOnSuccess(onModelLoaderComplete);
			}
			else {
				this._load3DComplete = true;
			}
			
			// load 2d images
			this._imageXmlList = this._confXml.load.images;
			
			trace("find " + this._confXml.load.image_number.toString() + " images.");
			
			this._imageNum = parseInt(this._confXml.load.image_number.toString());
			this._imageLoadedNum = 0;
			
			if (this._imageNum > 0) {
				this._imageLoader = new Loader();
				
				str = this._imageXmlList.image[this._imageLoadedNum].toString()
				trace("loading image: " + str);
				this._imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaderComplete);
				this._imageLoader.load(new URLRequest(str));
			}
			else {
				this._load2DComplete = true;
			}
		}
		
		public function onModelLoaderComplete(e:Event):void {
			trace("push model to map: " + this._modelXmlList.model[this._modelLoadedNum].@id);
			this._modelMap[this._modelXmlList.model[this._modelLoadedNum].@id] = this._modelLoader.handle.clone();
			
			this._modelLoadedNum++;
			
			if (this._modelLoadedNum == this._modelNum) {
				this._load3DComplete = true;
			}
			else {
				var str:String = this._modelXmlList.model[this._modelLoadedNum].toString()
				
				trace("loading model: " + str);
				this._modelLoader = Collada.load(str);
				this._modelLoader.addOnSuccess(onModelLoaderComplete);
			}
		}
		
		public function onImageLoaderComplete(e:Event):void {
			var loadedImage:Bitmap = Bitmap(this._imageLoader.content);
			
			var bitmap:BitmapData = new BitmapData(loadedImage.width, loadedImage.height, true, 0x00ffffff);
			bitmap.draw(loadedImage, new Matrix());
			
			trace("push image to map: " + this._imageXmlList.image[this._imageLoadedNum].@id);
			this._imageBitmapMap[this._imageXmlList.image[this._imageLoadedNum].@id] = bitmap;
			
			this._imageLoadedNum++;
			
			if (this._imageLoadedNum == this._imageNum) {
				this._load2DComplete = true;
			}
			else {
				this._imageLoader = new Loader();
				
				var str:String = this._imageXmlList.image[this._imageLoadedNum].toString()
				trace("loading image: " + str);
				this._imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaderComplete);
				this._imageLoader.load(new URLRequest(str));
			}
		}
		
	}
	
}